package application.services;

import application.exceptions.ItemNotFoundException;
import application.model.ProducentsModel;
import application.model.modify.ProducentCompositionModel;
import application.model.modify.ProducentModel;
import application.translators.Translator;
import application.translators.impl.ProducentsTranslator;
import dao.ProducentCompositionDao;
import mappings.Producent;
import mappings.ProducentComposition;
import org.hibernate.exception.ConstraintViolationException;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("producentCompositions")
public class ProducentCompositionService
{
    Translator<List<Producent>, ProducentsModel> producentTranslator = new ProducentsTranslator();
    ProducentCompositionDao producentCompositionDao = new ProducentCompositionDao();

    @Path("getForMaterial/{materialId}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public ProducentsModel getProducentsForMaterialId(@PathParam("materialId") int materialId)
    {
        return producentTranslator.translate(producentCompositionDao.getProducentsForMaterial(materialId));
    }

    @Path("addProducentComposition")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public ProducentCompositionModel addProducentComposition(@FormParam("materialId") int materialId,
                                                            @FormParam("producentId") int producentId)
    {
        ProducentCompositionModel producentCompositionModel = new ProducentCompositionModel();

        try
        {
            ProducentComposition producentComposition = producentCompositionDao.addProducentComposition(materialId, producentId);
            producentCompositionModel.setId(producentComposition.getId());
            producentCompositionModel.setMaterialName(producentComposition.getMaterial().getName());
            producentCompositionModel.setProducentName(producentComposition.getProducent().getName());
            producentCompositionModel.setStatus(0);
        }
        catch (ConstraintViolationException e)
        {
            // lapiemy wyjatek rzucany przez hibernate'a dla unique constraint - ustawiamy status 1 (rekord istnieje)
            producentCompositionModel.setStatus(1);
        }
        catch (Exception e)
        {
            // lapiemy wszystkie inne exceptiony i ustawiamy status na 2 (czyli cos sie wyjebalo, ale nie wiadomo co :P)
            producentCompositionModel.setStatus(2);
        }

        return producentCompositionModel;
    }

    @Path("editProducentComposition")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public ProducentCompositionModel editProducentComposition(@FormParam("id") int id,
                                                   @FormParam("materialId") int materialId,
                                                   @FormParam("producentId") int producentId)
    {
        ProducentCompositionModel producentCompositionModel = new ProducentCompositionModel();

        try
        {
            ProducentComposition producentComposition = producentCompositionDao.editProducentComposition(id, materialId, producentId);
            producentCompositionModel.setId(producentComposition.getId());
            producentCompositionModel.setStatus(0);
            producentCompositionModel.setProducentName(producentComposition.getProducent().getName());
            producentCompositionModel.setMaterialName(producentComposition.getMaterial().getName());
        }
        catch (ItemNotFoundException e)
        {
            producentCompositionModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            producentCompositionModel.setStatus(2);
        }

        return producentCompositionModel;
    }

    @Path("deleteProducentComposition")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public ProducentCompositionModel deleteProducentComposition(@FormParam("id") Integer id)
    {
        ProducentCompositionModel producentCompositionModel = new ProducentCompositionModel();

        try
        {
             producentCompositionDao.deleteProducentComposition(id);
             producentCompositionModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            producentCompositionModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            producentCompositionModel.setStatus(2);
        }

        return producentCompositionModel;
    }
}
